<?php
//UMA
namespace App\Exceptions;

use Exception;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Validation\ValidationException;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that are not reported.
     *
     * @var array
     */
    protected $dontReport = [
        //
    ];

    /**
     * A list of the inputs that are never flashed for validation exceptions.
     *
     * @var array
     */
    protected $dontFlash = [
        'password',
        'password_confirmation',
    ];

    /**
     * Report or log an exception.
     *
     * @param \Exception $exception
     * @return void
     */
    public function report(Exception $exception)
    {
        parent::report($exception);
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Exception $exception
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $exception)
    {
        if ($exception instanceof HttpResponseException) {
            \Log::debug('is http exception');
            return response()->json($exception->getMessage());
        }
        if ($request->is("api/*")) {
            if ($exception instanceof ValidationException) {
                $result = [
                    "code" => 422,
                    "msg" => 'Validation Failed',
                    "data" => array_combine(array_keys($exception->errors()), array_column($exception->errors(), 0))
                ];
                return response()->json($result, 422);
            }
        }
        return parent::render($request, $exception);
    }
}
